package com.hxht.storage.server.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import javax.annotation.PostConstruct;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;

/**
 * 从DBSettings.xml配置文件中读取服务器数据库类型信息
 * @author zhouyg
 *
 */
//@Component
public class ReadDBTypeUtil {
	
	private static final Logger log = Logger.getLogger(ReadDBTypeUtil.class);
	
//	public static final String MYSQL_DIALECT = "org.hibernate.dialect.MySQLInnoDBDialect";
//	
//	public static final String DM_DIALECT = "org.hibernate.dialect.DmDialect";
//	
//	public static final String OSCAR_DIALECT = "org.hibernate.dialect.OscarDialect";
//	
//	private Map<String, String> dialects = new HashMap<String, String>();
	
	private String userName;
	
	private String pwd;
	
	private String driver;
	
	private String jdbcUrl;
	
	private String ip;
	
	private String port;
	
	private String dialect;
	
	//#{readDBType.dutyDataSource}
	private String dutyDataSource;
	
	//#{readDBType.nvmpDataSource}
	private String nvmpDataSource;	
	
	public ReadDBTypeUtil(){
//		dialects.put("dm", DM_DIALECT);
//		dialects.put("oscar", OSCAR_DIALECT);
//		dialects.put("mysql", MYSQL_DIALECT);
	}
	
	@PostConstruct
	/**
	 * 从/etc/fxconf/DB/DBSettings.xml中 读取DBType指
	 * 		mysql : mysql数据库
	 * 		oscar : 神通数据库
	 * 		dm	  : 达梦数据库
	 * @return
	 */
	public void init(){
		
		String dbType = null;
		
		try {
			String dbSettinsPath = "";
			if (System.getProperty("os.name").equals("Linux")) {
				dbSettinsPath = "/etc/fxconf/DB/DBSettings.xml";
//				dbSettinsPath = "/etc/fxconf/DB/Storage.xml";
			} else {
				dbSettinsPath = "D:\\fxconf\\DB\\DBSettings.xml";
			}

			SAXReader reader = new SAXReader();
			// 获取xml文件的输入流
			InputStream inStream = new FileInputStream(new File(dbSettinsPath));
			//取得代表文档的Document对象  
			Document document = reader.read(inStream);  
			//取得根结点  
			Element root = document.getRootElement(); 
			
			dbType = root.elementTextTrim("DBType");
			dialect = root.elementTextTrim("Dialect");
			ip = root.elementTextTrim("IP");
			port = root.elementTextTrim("Port");
			userName = root.elementTextTrim("SN");
			pwd = root.elementTextTrim("PN");
			driver = root.elementTextTrim("Driver");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			log.error("db settings file does't find!");
		}catch (DocumentException e) {
			// TODO Auto-generated catch block
			log.error("load db settings xml file error!");
		}  
		
		if (StringUtils.isEmpty(dbType)){
			dbType = "mysql";
		}
		
		if (dbType.equalsIgnoreCase("oscar")){
			jdbcUrl = "jdbc:oscar://" + ip + ":" + port + "/nvmp?useUnicode=true&characterEncoding=GBK&autoReconnect=true&failOverReadOnly=false";
		} else if (dbType.equalsIgnoreCase("dm")) {
			jdbcUrl = "jdbc:dm://" + ip + ":" + port + "/nvmp?useUnicode=true&characterEncoding=GBK&autoReconnect=true&failOverReadOnly=false";
		} else {
			jdbcUrl = "jdbc:mysql://" + ip + ":" + port + "/nvmp?useUnicode=true&characterEncoding=GBK&autoReconnect=true&failOverReadOnly=false";
		}
		
//		dutyDataSource = "dutyDataSource_" + dbType;
//		nvmpDataSource = "nvmpDataSource_" + dbType;
	}

	public String getDutyDataSource() {
		return dutyDataSource;
	}

	public void setDutyDataSource(String dutyDataSource) {
		this.dutyDataSource = dutyDataSource;
	}

	public String getNvmpDataSource() {
		return nvmpDataSource;
	}

	public void setNvmpDataSource(String nvmpDataSource) {
		this.nvmpDataSource = nvmpDataSource;
	}

	public String getDialect() {
		return dialect;
	}

	public void setDialect(String dialect) {
		this.dialect = dialect;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driverClass) {
		this.driver = driverClass;
	}

	public String getJdbcUrl() {
		return jdbcUrl;
	}

	public void setJdbcUrl(String jdbcUrl) {
		this.jdbcUrl = jdbcUrl;
	}

	
}
